package com.self.baseutile.Utile;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;

import com.self.baseutile.MRWApplication;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Craeted by: huangdx
 * Date : 2021/3/16
 * Describe :
 */
public class StringUtils {

    private StringUtils() {
        throw new AssertionError();
    }

    public static String getVersionName()
    {
        // 获取packagemanager的实例
        PackageManager packageManager = MRWApplication.mContext.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        String version = "0";
        try {
            PackageInfo packInfo = packageManager.getPackageInfo(MRWApplication.mContext.getPackageName(),0);
            version = packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return version;
    }
    /**
     * 手机号码加密
     *
     * @param
     * @return
     */
    public static  String phoneScr(String phone){
        if(phone!=null){
            return  phone.substring(0, 3) + "****" + phone.substring(7, 11);
        }else {
            return "";
        }

    }

    //是否为支付码
    public static boolean isPaidCode(String str) throws PatternSyntaxException {
        String regExp = "/^(?:1[0-5]\\d{16}|(?:2[5-9]|30)\\d{14,18})$/";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }


    /**
     * 纯数字
     *
     * @param str
     * @return
     */

    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     *
     * 计算相隔多少天
     *
     * */
    public static int dayCompare(Date fromDate, Date toDate){
        Calendar from  =  Calendar.getInstance();
        from.setTime(fromDate);
        Calendar  to  =  Calendar.getInstance();
        to.setTime(toDate);
        int day = (int) ((to.getTimeInMillis()  -  from.getTimeInMillis())  /  (24  *  3600  *  1000));
        return day;
    }
    /**
     * jpg png bmp 彩色图片转换Bitmap数据为int[]数组
     * @param bm
     * @return int[]
     */
    public static int[] getPixelsByBitmap(Bitmap bm) {
        int width, heigh;
        width = bm.getWidth();
        heigh = bm.getHeight();
        int iDataLen = width * heigh;
        int[] pixels = new int[iDataLen];
        bm.getPixels(pixels, 0, width, 0, 0, width, heigh);
        return pixels;
    }

    /**
     * 获取单色位图
     */
    public static Bitmap getSinglePic(Bitmap inputBMP) {
        int[] pix = new int[inputBMP.getWidth() * inputBMP.getHeight()];
        int[] colorTemp = new int[inputBMP.getWidth() * inputBMP.getHeight()];
        inputBMP.getPixels(pix, 0, inputBMP.getWidth(), 0, 0,
                inputBMP.getWidth(), inputBMP.getHeight());
        Bitmap returBMP = Bitmap.createBitmap(inputBMP.getWidth(),
                inputBMP.getHeight(), Bitmap.Config.RGB_565);
        int lightNumber = 127;//曝光度，這個顔色是中間值，如果大於中間值，那就是黑色，否則白色，数值越小，曝光度越高
        for (int j = 0; j < colorTemp.length; j++) {
            colorTemp[j] = Color.rgb(Color.red(pix[j]),
                    Color.green(pix[j]), Color.blue(pix[j]));
        }
        for (int i = 0; i < colorTemp.length; i++) {
            int r = Color.red(pix[i]);
            int g = Color.green(pix[i]);
            int b = Color.blue(pix[i]);
            if (r + g + b > 3 * lightNumber) {//三种颜色相加大于3倍的曝光值，才是黑色，否则白色
                colorTemp[i] = Color.rgb(255, 255, 255);
            } else {
                colorTemp[i] = Color.rgb(0, 0, 0);
            }
        }
        returBMP.setPixels(colorTemp, 0, inputBMP.getWidth(), 0, 0,
                inputBMP.getWidth(), inputBMP.getHeight());
        return returBMP;
    }
}
